package com.um.jdy.platform.platform.manager;

import com.um.jdy.common.platform.entity.dto.CompanyRoleDTO;
import com.um.jdy.common.platform.entity.po.CompanyMenu;
import com.um.jdy.common.platform.entity.po.CompanyRole;
import com.um.jdy.common.platform.entity.po.CompanyRoleMenu;
import com.um.jdy.common.platform.service.CompanyMenuService;
import com.um.jdy.common.platform.service.CompanyRoleMenuService;
import com.um.jdy.common.platform.service.CompanyRoleService;
import com.um.jdy.platform.platform.entity.vo.CompanyMenuNode;
import com.um.jdy.platform.platform.entity.vo.CompanyMenuTreeNode;
import com.um.jdy.platform.platform.service.PlatformMenuTreeService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 管理类：平台角色表
 */
@Component
public class CompanyRoleManager extends BaseManager<CompanyRoleService> {

    @Autowired
    CompanyMenuService companyMenuService;

    @Autowired
    PlatformMenuTreeService platformMenuTreeService;

    @Autowired
    CompanyRoleMenuService companyRoleMenuService;

    /**
     * 分页查询站场
     * @param params
     * @return
     */
    public Response<Page<CompanyRole>> getPage(PageParams params){
        Page page = new Page();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());
        page = service.getPage(page,params.getProperties(), params.getValues(),params.getRangeEntitys(),params.getOrderNames(),params.getSortOns());

        return Response.success(page);
    }

    public Response<CompanyMenuTreeNode> getMenuTree(String roleId){

        List<CompanyMenu> listMenu = companyMenuService.getRows(new String[]{},new Object[]{},"sort_no", "asc");
        List<CompanyMenuNode> listNode = UMBeanUtils.copyBeanList(listMenu,CompanyMenuNode.class);

        List<CompanyMenu> lstSelected = companyRoleMenuService.getViewModelList("platform_get_role_menu",CompanyMenu.class,new Object[]{roleId,getComId()});

        Map<String,String> mapSelected = new Hashtable<String,String>();
        for(CompanyMenu item : lstSelected){
            mapSelected.put(item.getPlatformCompanyMenuId(),item.getPlatformCompanyMenuId());
        }

        for(CompanyMenuNode item : listNode){
            if(mapSelected.containsKey(item.getPlatformCompanyMenuId())){
                item.setSelected(true);
            }
        }

        List<CompanyMenuTreeNode> nodes = platformMenuTreeService.toTreeNodeList(listNode);
        CompanyMenuTreeNode menuTreeNode = platformMenuTreeService.listToTreeNode("0", nodes);
        return Response.success(Optional.ofNullable(menuTreeNode).orElseGet(CompanyMenuTreeNode::new));

    }

    @Transactional
    public Response addEntity(CompanyRoleDTO roleDTO, List<String> menuIds){
        CompanyRole role = UMBeanUtils.copyBean(roleDTO,CompanyRole.class);
        role.setCreateTime(new Date());
        service.addEntity(role);

        for(String menuId:menuIds){
            CompanyRoleMenu item = new CompanyRoleMenu();
            item.setPlatformCompanyRoleId(role.getPlatformCompanyRoleId());
            item.setPlatformCompanyMenuId(menuId);
            companyRoleMenuService.addEntity(item);
        }
        return Response.success();
    }

    @Transactional
    public Response updateEntity(String id,CompanyRoleDTO roleDTO, List<String> menuIds){
        CompanyRole role = UMBeanUtils.copyBean(roleDTO,CompanyRole.class);
        role.setPlatformCompanyRoleId(id);
        service.updateEntity(role);

        companyRoleMenuService.delete("platform_company_role_id",id);
        for(String menuId:menuIds){
            CompanyRoleMenu item = new CompanyRoleMenu();
            item.setPlatformCompanyRoleId(role.getPlatformCompanyRoleId());
            item.setPlatformCompanyMenuId(menuId);
            companyRoleMenuService.addEntity(item);
        }
        return Response.success();
    }

    @Transactional
    public Response delete(String id) {
        boolean result = this.service.delete(id);
        companyRoleMenuService.delete("platform_company_role_id",id);
        return result ? Response.success() : Response.failed();
    }

    @Transactional
    public Response deleteIds(List<String> ids) {
        boolean result = service.deleteIds(ids);
        ids.stream().forEach(id -> {
            companyRoleMenuService.delete("platform_company_role_id",id);
        });
        return result ? Response.success() : Response.failed();
    }
}
